/**
 * ---------------------------
 * Time: 2017/5/18 21:41.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
var CanvasModel = Main.createConfigClass({
    default: {
        defaultVar: function () {
            return {
                modelImg: null,
                color: null,
                effectMap: {},
                effectInService: []
            }
        },
        defaultConfig: function () {
            return {
                type: 'image',
                isFaceDeg: true,
                size: 40,
                color: {type: 'random'},
                rotate: {
                    isRotate: false,
                    frameDeg: 0
                },
                effectMap: {
                    running: {
                        type: 'particles',
                        effect: {
                            width: 5,
                            height: 5,
                            gradient: {
                                rate: 0.5
                            }
                        }
                    }
                },
                realWidth: 40,
                realHeight: 40,
                degFaced: 0
            }
        },
        onConfigured: function () {
            this.size = this.config.size;
            this.isFaceDeg = this.config.isFaceDeg;
            this.type = this.config.type;
            this.rotate = this.config.rotate;
            this.realWidth = this.config.realWidth;
            this.realHeight = this.config.realHeight;
            this.degFaced = this.config.degFaced;
            switch (this.type) {
                case 'image':
                    this._configImage();
                    break;
                case 'rect':
                    this._configRect();
                    break;
                default:
                    break;
            }
        },
        _configEffect: function () {
            var _this = this;
            this.config.effectMap.map(function (key) {
                // 实例化特效
                _this.effectMap[key] = Cielar.newClass(
                    CanvasEffect,
                    [_this.config.effectMap[key]],
                    // 为特效指定父级为当前模型
                    {parent: _this.parent}
                );
                // 当特效消失时移出渲染队列
                _this.effectMap[key].onDestroy = function () {
                    _this.removeEffectInService(key);
                }
            });
        },
        _configImage: function () {
            var _this = this;
            if (this.config.img != null) {
                this.modelImg = new Image();
                this.modelImg.src = this.config.img.src;
                // 待图片加载完毕锁定图片长宽
                this.onLoadingOver(function () {
                    _this.realWidth = _this.size;
                    _this.realHeight = _this.realWidth * (_this.modelImg.height / _this.modelImg.width);
                });
            }
        },
        _configRect: function () {
            this.color = new Cielar.Color(this.config.color);
        },
        onInitFinish: function () {
            this.isReady = true;
            this._configEffect();
        }
    },
    defaultPrototype: {
        isLoadingOver: function () {
            return this.modelImg ? this.modelImg.complete : false;
        },
        getResultConfig: function () {
            if (!this.isReady) {
                return {};
            }
            if (this.isLoadingOver()) {
                return {
                    img: this.modelImg,
                    width: this.realWidth,
                    height: this.realHeight,
                    isFaceDeg: this.isFaceDeg,
                    rotate: this.rotate,
                    type: this.type
                }
            }
            return {
                width: this.realWidth,
                height: this.realHeight,
                isFaceDeg: this.isFaceDeg,
                rotate: this.rotate,
                type: this.type,
                color: this.color ? this.color.getRGBA() : null
            };
        },
        onLoadingOver: function (callback) {
            var _this = this;
            if (this.isLoadingOver()) {
                callback.call(this.modelImg);
                return;
            }
            this.modelImg.onload = function () {
                callback.call(_this.modelImg);
            };
        },
        playEffect: function (option) {
            if (this.hasEffect(option)) {
                this.effectMap[option].startEffect();
                this.addEffectInService(option);
            }
        },
        pauseEffect: function (option) {
            if (this.hasEffect(option)) {
                this.effectMap[option].stopEffect();
                this.removeEffectInService(option);
            }
        },
        getEffectConfig: function (option) {
            if (this.hasEffect(option)) {
                return this.effectMap[option].getDrawConfig();
            } else return {}
        },
        hasEffect: function (name) {
            return this.effectMap.hasOwnProperty(name);
        },
        hasEffectInService: function (name) {
            return this.effectInService.indexOf(name) != -1 && this.hasEffect(name);
        },
        addEffectInService: function (name) {
            if (this.hasEffect(name) && !this.hasEffectInService(name)) {
                return this.effectInService.push(name);
            } else {
                return -1;
            }
        },
        removeEffectInService: function (name) {
            if (this.hasEffectInService(name)) {
                var index = this.effectInService.indexOf(name);
                this.effectInService.splice(index, 1);
                return true;
            } else {
                return false;
            }
        }
    }
});